#define LSA_PCM_NEW_HW_PARAMS_API

//#include <stdio.h>

//#include <stdlib.h>

//#include <errno.h>

#include

#include

#include

//#include <lame/lame.h>

static int recording;

void stop_recording(int param){

    recording =0;

}

int main(intargs,char*argv[]){

    FILE * pFile;

    pFile = fopen("test.pcm","wb");

    intrc;

    intsize;

    snd_pcm_t *handle;

    snd_pcm_hw_params_t *params;

    unsigned int val;

    intdir;

    snd_pcm_uframes_t frames;

    char* buffer;

    //int snd_pcm_open(snd_pcm_t **pcm, const char *name,

      //      snd_pcm_stream_t stream, int mode);

    //Open the pcm device for recording

    rc = snd_pcm_open(&handle,"default", SND_PCM_STREAM_CAPTURE,0);

    if( rc <0){

        fprintf(stderr,"unabble to open pcm device : %s\n", snd_strerror(rc));

        exit(1);

    }

    //Allocate a hardware parameters object

    snd_pcm_hw_params_alloca(�0�9ms);

    //Fill it in with default values

    snd_pcm_hw_params_any(handle, params);

    //set the desired hardware parameters

    //Interleaved mode

    snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_MMAP_INTERLEAVED);

    //Signed 16-bit little-endian format

    snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);

    //Two channels

//    snd_pcm_hw_params_set_channels(handle, params, 2);

    //One channel

    snd_pcm_hw_params_set_channels(handle, params,1);

    //44100 bits/seconds sampling rate(CD quality)

    val =8800;

    snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);

    //Set period size to 32 frams

    frames =32;

    snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);

    //Write the parameters to  the driver

    rc = snd_pcm_hw_params(handle, params);

    if( rc <0){

        fprintf(stderr,"Unable to set hw paramters: %s\n", snd_strerror(rc));

        exit(1);

    }

    //Use a buffer large enough to hold one period

    snd_pcm_hw_params_get_period_size(params, &frames, &dir);

    //Use the 16 bbit and double channel, so need *4

//    size = frames * 4;  // 2 bytes/sample, 2 channels

    size = frames *2;  // 2 bytes/sample, 2 channels

    buffer = (char*)malloc(size);

    //We want to loop for 5 seconds

    snd_pcm_hw_params_get_period_time(params, &val, &dir);

    recording =1;

    while(recording){

        //Catch datas

        rc = snd_pcm_readi(handle, buffer, frames);

        if(rc == -EPIPE){

            //EPIPE means overrun

            fprintf(stderr,"overrun occured\n");

            snd_pcm_prepare(handle);

        }elseif( rc <0){

            fprintf(stderr,"error from read: %s\n", snd_strerror(rc));

        }elseif(rc != (int)frames){

            fprintf(stderr,"short read, read %d frames\n", size);

        }

        printf("write to file ....... %d\n", size);;

        //Write to the generic output

        rc = fwrite(buffer,sizeof(char), size, pFile);

        if(rc != size){

            fprintf(stderr,"short write:wrote %d bytes\n", rc);

        }

        if(signal(SIGINT, stop_recording) == SIG_ERR){

            fprintf(stderr,"signal() failed\n");

        }

    }

    snd_pcm_drain(handle);

    snd_pcm_close(handle);

    free(buffer);

    fclose(pFile);

    return0;

}
